package com.stores.unitTests.admin;

import static org.junit.Assert.*;

import java.sql.SQLException;

import org.junit.Test;

import com.stores.unitTests.LocalDatabase;
import com.stores.v1.entity.*;
import com.stores.v1.model.*;
import com.stores.v1.security.*;
import com.stores.v1.toolset.*;

public class PersonFixture {

	private PersonFixtureHelper helper = new PersonFixtureHelper();

	@Test
	public void shouldCreateASiteAdminUsingSiteAdmin()
			throws DatabaseInitializationException,
			DatabaseTransactionException, AccessControlException,
			DatabaseCommandException, UserAccountNullException {

		SiteAdmin admin = new SiteAdmin();
		admin.setAccount(new UserAccount());
		admin.getAccount().setId(1);

		int id = helper.createSiteAdminUser(admin);

		helper.deleteUser(id);

		assertTrue(id > 0);
	}

	@Test
	public void canStoreManagerAddNewStoreManager()
			throws DatabaseInitializationException, DatabaseCommandException {
		IAccessControl control = new AccessControl();
		int privilegeId = AccessList.ADD_NEW_STORE_MANAGER;
		control.setDatabase(new LocalDatabase());
		control.setUserContext(new StoreManager());
		control.setPrivilegeId(privilegeId);
		boolean isAllowed = control.isAllowed();
		assertTrue(!isAllowed);
	}

	@Test
	public void canSiteManagerAddNewStoreManager()
			throws DatabaseInitializationException, DatabaseCommandException,
			DatabaseTransactionException, AccessControlException,
			UserAccountNullException, SQLException {
		SiteManager userContext = new SiteManager();
		userContext.setId(10);
		userContext.setAccount(new UserAccount());
		userContext.getAccount().setId(1);
		SiteManagerModel model = new SiteManagerModel(userContext);
		AccessControl control = new AccessControl();
		control.setPrivilegeId(AccessList.ADD_NEW_STORE_MANAGER);
		model.setAccessControl(control);
		model.setDatabase(new LocalDatabase());
		int userId = model.createUser(helper.getNewStoreManager());
		model.addToActivityLog(userContext.getId(),
				AccessList.ADD_NEW_STORE_MANAGER);
		// model.deleteUser(user)
		helper.deleteUser(userId);
	}

	// @Test
	// public void createNewSiteManager() throws
	// DatabaseInitializationException,
	// DatabaseTransactionException, AccessControlException,
	// DatabaseCommandException {
	// SiteAdmin userContext = new SiteAdmin();
	// userContext.setAccount(new UserAccount());
	// userContext.getAccount().setId(5);
	// SiteAdminModel model = new SiteAdminModel(userContext);
	// model.setDatabase(new LocalDatabase());
	// model.createUser(helper.getNewSiteManager());
	// }
}
